package view;

import dao.Blog;
import dao.BlogDao;
import dao.User;
import session.Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
@WebServlet("/insert")
public class Insert extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = Session.isLogin(req);
        if (user == null){
            resp.sendRedirect("login.html");
            return;
        }
        req.setCharacterEncoding("utf-8");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setPostTime(new Timestamp(System.currentTimeMillis()));
        blog.setUserId(user.getUserId());
        System.out.println(user.getUserId());
        BlogDao blogDao = new BlogDao();
        try {
            blogDao.insert(blog);
            System.out.println("插入成功");
            resp.sendRedirect("blogList.html");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
